package com.jp.knowledge.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public abstract class JPBaseAdpter<T> extends BaseAdapter {

    private Context mContext;
    protected List<T> mInfos = new ArrayList<>();

    public JPBaseAdpter(Context context, List<T> infos) {
        this.mContext = context;
        this.mInfos = infos;
    }

    public List<T> getDatas(){
        return mInfos;
    }

    // 根据Data内容更新
    public void updataAdapter(List<T> infos) {
        this.mInfos = infos;
        this.notifyDataSetChanged();
    }

    // 移除指定内容项
    public void removeItem(int position) {
        this.mInfos.remove(position);
        this.notifyDataSetChanged();

    }

    // 针对TextView的文本设置
    protected void setValue(int textViewId, String str, JPViewHolder holder) {
        holder.setText(textViewId, str);
    }

    public Context getContext() {
        return mContext;
    }

    @Override
    public int getCount() {
        return mInfos==null?0:mInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return mInfos==null?null:mInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        JPViewHolder holder = JPViewHolder.get(mContext, getLayoutId(position), parent, convertView);
        renderData(holder, (T) getItem(position), position);
        return holder.getConvertView();
    }

    // 获取Item的布局
    protected abstract int getLayoutId(int position);

    // 相当于getView()方法
    protected abstract void renderData(JPViewHolder holder, T item, int position);
}
